import 'dart:async';
import 'dart:io';

import 'package:acaleph/common/constant.dart';
import 'package:acaleph/model/base_model.dart';
import 'package:acaleph/model/test_detail_model.dart';
import 'package:acaleph/mvp/presenter/test_detail_presenter.dart';
import 'package:acaleph/mvp/presenter/test_detail_presenter_impl.dart';
import 'package:acaleph/widget/common_loading_dialog.dart';
import 'package:acaleph/widget/common_retry.dart';
import 'package:acaleph/widget/common_snakeBar.dart';
import 'package:flutter/material.dart';

class TestDetailPage extends StatefulWidget{
  final String id;


  TestDetailPage(this.id);

  @override
  State<StatefulWidget> createState() {
    _TestDetailState view = new _TestDetailState();
    TestDetailPresenter presenter = new TestDetailPresenterImpl(view);
    presenter.init();
    return view;
  }

}

class _TestDetailState extends State<TestDetailPage> implements TestDetailView{

  String _title = Constant.storyTitle;

  TestDetailPresenter _testDetailPresenter;

  TestDetailModel _testDetailModel;

  List<Widget> widgets = [];

  bool _isShowRetry = false;

  @override
  void initState() {
    super.initState();
    _refreshData();
  }

  Future<Null> _refreshData() {
    final Completer<Null> completer = new Completer<Null>();

    _testDetailPresenter.loadTestDetail(widget.id);

    completer.complete(null);

    return completer.future;
  }

  Widget _buildList(BuildContext context) {
    widgets.clear();
    if (null != _testDetailModel && null != _testDetailModel.title) {
      widgets.add(_buildTips());
    }
    var content;

    print("-----_buildList----"+_testDetailModel.toString());

    if (null == _testDetailModel) {
      if (_isShowRetry) {
        _isShowRetry = false;
        content = CommonRetry.buildRetry(_refreshData);
      } else {
        content = ProgressDialog.buildProgressDialog();
      }
    } else {
      content = new Stack(
        children: <Widget>[
          new ListView(
            children: widgets,
          ),
        ],
      );
    }
    return content;
  }

  Widget _buildTips() {
    return new Padding(
        padding: const EdgeInsets.all(12.0),
        child: new Container(
          child: new Center(
            child: new Text(
              _testDetailModel.title+"  "+_testDetailModel.sortname,
              style: new TextStyle(
                fontSize: 16.0,
              ),
            ),
          ),
        ));
  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.white,
      appBar: new AppBar(
        title: new Text('$_title'),
      ),
      body: _buildList(context),

    );
  }

  @override
  void onLoadTestDetailFail() {
    _isShowRetry = true;
    print("---onLoadTestDetailFail---");
    setState(() {});
  }

  @override
  void onLoadTestDetailSuc(BaseModel<TestDetailModel> model) {
    if(!mounted) return ;//异步处理，防止报错
    if (model.code != HttpStatus.OK) {
      CommonSnakeBar.buildSnakeBar(context, model.errorMsg);
      return;
    }
    _testDetailModel = model.data;
    _title = _testDetailModel.title;

    print("---onLoadTestDetailSuc---"+_testDetailModel.toString());
    setState(() {});
  }

  @override
  setPresenter(TestDetailPresenter presenter) {
    _testDetailPresenter = presenter;
  }

}